package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.ConmentService;
import com.dongdongshop.vo.ConmentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
@CrossOrigin //跨域注解
@Controller
@RequestMapping("conment")
public class ConmentController {

    @DubboReference(check = false)
    private ConmentService conmentService;

    @RequestMapping("addConment")
    @ResponseBody
    public Result addConment(ConmentVo conmentVo){
        try {
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            conmentVo.setUserId(user.getUsername());
            conmentVo.setNowDate(new Date());
            conmentVo.setZan("0");
            conmentService.addConment(conmentVo);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
    @RequestMapping("findAll")
    @ResponseBody
    public Result findAll(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "3") Integer pageSize,
                          String rank){
        try {
            PageResult<ConmentVo> result = conmentService.findAll(pageNum,pageSize,rank);
            return Result.OK().setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }
    @RequestMapping("updataZan")
    @ResponseBody
    public Result updataZan(String id){
        try {
            conmentService.updataZanById(id);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }
}
